import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'
import {verifyToken} from '@/api/user'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)

  // determine whether the user has logged in
  // const hasToken = getToken()

  // 判断用户是否登录
  verifyToken().then(res =>{
    // console.log(res);
    if(!res.success){ //令牌无效
      if(to.path === '/login' ){
        next()
      }else{
        next(`/login?redirect=${to.path}`)
        NProgress.done()
      }
    }else{
      next()
    }
  })
  
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
